package mSocket;

import java.io.*;
import java.net.InetAddress;
import java.net.Socket;
import java.util.Scanner;

public class SocketClient2 {
	public static void main(String args[]) throws Exception {
		// 要连接的服务端IP地址和端口
//		String host = "127.0.0.1";
		String host = InetAddress.getLocalHost().getHostAddress();
		int port = 10899;
		System.out.println("i am client connect "+host+port);
		// 与服务端建立连接
		MyClient myClient = new MyClient(host, port);
		while (true){
			Scanner scanner = new Scanner(System.in);
			myClient.send(scanner.next());
		}
	}

}
class MyClient {
	protected String host;
	protected int port;
	protected Socket socket;
	protected PrintWriter outputWriter;
	protected BufferedReader inputReader;

	public MyClient(String host, int port) throws IOException {
		socket = new Socket(host, port);
		InetAddress inetAddress = socket.getInetAddress();
		System.out.println("inetAddress "+inetAddress);
		outputWriter = new PrintWriter(socket.getOutputStream());
		inputReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
	}

	public void send(final String message) throws IOException {
		outputWriter.print(message+"\n"); // important to add a newline
		outputWriter.flush();

		String responseStr = inputReader.readLine();
		System.out.println(responseStr);
//			if (StringUtils.isBlank(responseStr)) {
//				return;
	}
}
